package com.dongjiantong.user.serviceImpl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.common.utils.JsonUtil;
import com.dongjiantong.common.utils.MD5Util;
import com.dongjiantong.common.utils.StringUtil;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.sys.enums.LogsEnum;
import com.dongjiantong.sys.service.SysLogsService;
import com.dongjiantong.user.manager.UserManager;
import com.dongjiantong.user.mapper.BossUserMapper;
import com.dongjiantong.user.mapper.LoginUserMapper;
import com.dongjiantong.user.mapper.MzUserMapper;
import com.dongjiantong.user.model.LoginUser;
import com.dongjiantong.user.model.MzUser;
import com.dongjiantong.user.enums.UserEnum;
import com.dongjiantong.user.model.dtos.BossUserDto;
import com.dongjiantong.user.service.BossUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author cao
 * @create 2017-11-20 下午1:34
 **/
@Slf4j
@Service(version = "1.0.0")
public class BossUserServiceImpl extends ServiceMybatis<BossUserDto> implements BossUserService {

    @Resource
    private MzUserMapper mzUserMapper;

    @Resource
    private BossUserMapper bossUserMapper;

    @Resource
    private LoginUserMapper loginUserMapper;

    @Autowired
    private UserManager userManager;

    @Reference(version="1.0.0")
    private SysLogsService sysLogsService;


    @Override
    public PageInfo<BossUserDto> getBossListByPage(UserEnum userEnum,
                                                   Integer pageNum,
                                                   Integer pageSize,
                                                   Map<String,Object> searchMap)
    {

        PageHelper.startPage(pageNum, pageSize);

        List<BossUserDto> userList = bossUserMapper.getBossListByPage(searchMap);
        PageInfo<BossUserDto> pageInfo = new PageInfo<BossUserDto>(userList);
        return  pageInfo;
    }

    @Override
    public boolean createBossUsers(MzUser mzUser, LoginUser loginUser)
    {
        

        mzUserMapper.insert(mzUser);
        long userId = mzUser.getId();

        loginUser.setUserId(userId);
        loginUser.setSalt("xyz");
        loginUser.setStatus(0);
        loginUser.setLogincount(0);
        loginUser.setErrorcount(0);
        loginUser.setUnlocktime(new Date());

        //密码加密
        String newPassword= MD5Util.encrypt(loginUser.getSalt() + loginUser.getPassword());
        loginUser.setPassword(newPassword);


        loginUserMapper.insert(loginUser);

       // sysLogsService.createLogs(LogsEnum.ADD,"mz_user,login_users","","", JsonUtil.toJsonString(mzUser),1L);

        return userId>0;
    }

    /**
     * 修改未运维人员
     * @param mzUser
     * @param loginUser
     * @return
     */
    @Override
    public boolean editBossUsers(MzUser mzUser, LoginUser loginUser)
    {
        boolean result=true;
        //只能修改职位，身份证号，密码

        MzUser mzUser1 = mzUserMapper.selectByPrimaryKey(mzUser.getId());

        mzUser1.setTitle(mzUser.getTitle());
        mzUser1.setIdcard(mzUser.getIdcard());
        mzUser1.setUpdatedId(mzUser.getUpdatedId());
        mzUser1.setUpdatedAt(mzUser.getUpdatedAt());
       int i = mzUserMapper.updateByPrimaryKey(mzUser1);
       if(i<0)
        {
            result =false;
        }


        if(!StringUtil.isBlank(loginUser.getPassword()))
        {
            userManager.updatePassword(mzUser1.getId(),loginUser.getPassword());
        }

        return  result;
    }


}
